package com.kl.modular.sysman.system.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kl.modular.common.controller.BaseController;
import com.kl.modular.common.response.ResponseBase;
import com.kl.modular.system.dao.model.SystemParamType;
import com.kl.modular.system.service.api.ISystemParamTypeService;
import com.kl.modular.system.service.bo.SystemParamTypeBo;
import org.apache.commons.lang.StringUtils;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * 系统参数类别控制器
 * @author xr_zjb
 * @date 2017-08-09 13:48
 * @(C) xinrui Copyright 
 */
@Controller
public class SystemParamTypeController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemParamTypeController.class);
	
	@Autowired
	private ISystemParamTypeService systemParamTypeService;
	
	/** 系统参数类别 view */
	private static String pathSystemParamTypeRoot="/sysman/system/paramType/";
	
	
	/**
	 * 系统参数类别列表页面
	 */
	@RequestMapping(value = "/sysman/systemParamType/systemParamTypeList", method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemParam_man:type_view")
	public String queryList(HttpServletRequest request) {
		logger.info("系统参数类别列表页面");
		return pathSystemParamTypeRoot + "systemParamTypeList.ftl";
	}

	/**
	 * 系统参数类别列表数据
	 */
	@RequestMapping(value = "/sysman/systemParamType/systemParamTypeAjaxList", method = RequestMethod.POST)
	@RequiresPermissions("sysman:systemParam_man:type_view")
	public String queryAjaxList(SystemParamTypeBo systemParamTypeBo, Page<SystemParamType> page, HttpServletRequest request) {
		logger.info("systemParamType列表数据>>data:" + JSON.toJSONString(systemParamTypeBo));
		page = systemParamTypeService.selectPageBySystemDicTypeBo(page, systemParamTypeBo);
		request.setAttribute("pager", page);
		return pathSystemParamTypeRoot + "systemParamTypeAjaxList.ftl";
	}
	
	
	/**
	 * 系统参数类别添加页面
	 * @return
	 */
	@RequestMapping(value="/sysman/systemParamType/toSystemParamTypeAdd" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemParam_man:type_add")
	public String toAddSystemParamType(){
		logger.info("系统参数类别添加页面");
		return pathSystemParamTypeRoot + "addSystemParamType.ftl";
	}
	
	/**
	 * 添加系统参数类别
	 */
	@RequestMapping(value="/sysman/systemParamType/addSystemParamType" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemParam_man:type_add")
	public String addSystemParamType(@Valid SystemParamType systemParamType, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("添加系统参数类别>>data:" + JSON.toJSONString(systemParamType));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}

		systemParamType.setCreateTime(new Date());
		systemParamType.setModifyTime(new Date());
		systemParamTypeService.insert(systemParamType);

		String jsonstr = ResponseBase.success();
		logger.info("添加系统参数类别>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	
	/**
	 * 系统参数类别更新页面
	 * @return
	 */
	@RequestMapping(value = "/sysman/systemParamType/toChangeView" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemParam_man:type_update")
	public String toChangeSystemParamType(Long id, HttpServletRequest request) throws Exception{
		logger.info("系统参数类别修改页面>>data:" + id);
		SystemParamType systemParamType = systemParamTypeService.selectById(id);
		request.setAttribute("systemParamType", systemParamType);
		return pathSystemParamTypeRoot + "systemParamTypeUpChange.ftl";
	}

	
	/**	
	 * 修改
	 */
	@RequestMapping(value = "/sysman/systemParamType/upChange" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemParam_man:type_update")
	public String upChangeSystemParamType(@Valid SystemParamType systemParamType, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("更新系统参数类别信息>>data:" + JSON.toJSONString(systemParamType));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}
		if(systemParamType.getId() == null){
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		systemParamType.setModifyTime(new Date());
		systemParamTypeService.updateById(systemParamType);

		String jsonstr = ResponseBase.success();
		logger.info("编辑系统参数类别信息>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	/**
	 * 批量删除系统参数类别
	 */
	@RequestMapping(value = "/sysman/systemParamType/delSystemParamType" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemParam_man:type_delete")
	public String delSystemParamType(String id,HttpServletResponse response) throws Exception{
		logger.info("批量删除系统参数类别>>data:" + JSON.toJSONString(id));

		// 数据验证
		if (StringUtils.isEmpty(id)) {
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		String[] codestrs = id.split(",");

		systemParamTypeService.logicalDeleteBatch(codestrs);

		String jsonstr = ResponseBase.success();
		logger.info("删除系统参数类别资源>>返回view>>data:" + jsonstr);
		return jsonstr;
	}
}
